
#include "config.hpp"

Config* Config::m_pInstance = NULL;

Config::Config(){}

Config::~Config(){

	unsigned nSize	 = this->configList.size();
	unsigned i		 = 0;
	ConfigNode **vec = NULL;

	vec = ( ConfigNode** )malloc(( sizeof( ConfigNode* ) * nSize ));

	ListIterator<ConfigNode> it = this->configList.iterator();
	while(it.hasNext()){
		vec[i] = it.remove();
		i++;
	}

	for (i = 0; i < nSize; i++)
		delete vec[i];

	free(vec);
	vec = NULL;

}

void Config::addConfigItem(std::string &id, std::string &value){

	ConfigNode *newCNode = new ConfigNode(id,value);
	this->configList.add(newCNode);
}

std::string Config::getValue(std::string id){

	ConfigNode nodeTemp(id," ");
	ConfigNode *cNode = this->configList.search(&nodeTemp);
	std::string value;

	if(cNode)
		value = cNode->getValue();

	return value;
}

/**
 * TODO Nico: Agregar validacion apropiada de existencia.
 */
void Config::modifyConfigItem(std::string &id, std::string &value){

	ConfigNode nodeTemp(id," ");
	ConfigNode *cNode = this->configList.search(&nodeTemp);

	if(cNode)
		cNode->setValue(value);
}

/**
 * TODO Nico: Agregar validacion apropiada de existencia.
 */
void Config::removeConfigItem(std::string &id){

	ConfigNode nodeTemp(id," ");
	ConfigNode *cNode = this->configList.remove(&nodeTemp);
	
	if(cNode)
		delete cNode;
}

void Config::showConfig(){

	ListIterator<ConfigNode> it = this->configList.iterator();
	ConfigNode *cNode;
	while(it.hasNext()){
		cNode = it.next();
		std::cout << "id = " << cNode->getId() << ", value = " << cNode->getValue() << std::endl;
	}
}